package com.example.springboot.service;

import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.springboot.entity.User;
import com.example.springboot.exception.ServiceException;
import com.example.springboot.mapper.UserMapper;
import com.example.springboot.utils.TokenUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 功能：用户相关
 * 作者：刘涵茹
 * 日期：2024/2/14 13:49
 */
@Service//通过注解注入spring容器
public class UserService extends ServiceImpl<UserMapper, User> {

    @Resource
    UserMapper userMapper;

    /**
     * 默认姓名、密码
     */
    @Override
    public boolean save(User entity) {
        if (StrUtil.isBlank(entity.getUsername())){
            entity.setUsername("honey-" + RandomUtil.randomNumbers(4)); //没有设置姓名时默认姓名
        }
        if (StrUtil.isBlank(entity.getPassword())){
            entity.setPassword("123"); //没有设置密码时默认密码123
        }
        if (StrUtil.isBlank(entity.getRole())){
            entity.setRole("学生"); //没有设置角色时默认角色为学生
        }
        return super.save(entity);
    }

    //封装方法
    public User selectByUserid(String userid){
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();//条件查询器
        queryWrapper.eq("userid", userid); //eq 相当于 ==,
        // 该语句等于where username = #{username}
        //根据用户名查询数据库用户的信息
        return getOne(queryWrapper);//select * from user where username = #{username}
    }

    //登录
    //验证用户账户是否合法
    public User login(User user) {
        User dbUser = selectByUserid(user.getUserid());
        if (dbUser == null){
            //抛出一个自定义的异常
            throw new ServiceException("用户名或密码错误");
        }
        if (!user.getPassword().equals(dbUser.getPassword())){
            throw new ServiceException("用户名或密码错误");
        }
        //生成token
        String token = TokenUtils.createToken(dbUser.getId().toString(), dbUser.getPassword());
        dbUser.setToken(token);
        return dbUser;
    }

    //注册
    public User register(User user) {
        //根据账号查询数据库用户的信息
        User dbUser = selectByUserid(user.getUserid());
        //id验重
        if (dbUser != null){
            //抛出一个自定义的异常
            throw new ServiceException("该账号已存在");
        }
        //手机号验重
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();//条件查询器
        queryWrapper.eq("phone", user.getPhone()); //eq 相当于 ==,
        User dbPhone = getOne(queryWrapper);
        if (dbPhone != null){
            //抛出一个自定义的异常
            throw new ServiceException("该手机号已被使用");
        }
        //姓名默认
        user.setUsername("honey-" + RandomUtil.randomNumbers(4));//默认用户名随机数
//        user.setUsername(user.getUserid());//默认用户账号为用户名称
        userMapper.insert(user);
        return user;
    }


    //密码重置
    public void resetPassword(User user) {
        User dbUser = selectByUserid(user.getUserid());
        if (dbUser == null){
            //抛出一个自定义的异常
            throw new ServiceException("用户不存在!");
        }
        if (!user.getPhone().equals(dbUser.getPhone())){
            throw new ServiceException("验证错误");
        }
        dbUser.setPassword("123"); //重置密码
        updateById(dbUser);
    }

    public User getByPartId(Integer partid) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();//条件查询器
        queryWrapper.eq("id", partid); //eq 相当于 ==,
        return getOne(queryWrapper);//select * from user where username = #{username}
    }
}
